
$(function () {
    /*显示下载二维码*/
    $("#downloadapp").mouseenter(function () {
        $("#download-erweima").show();
    }).mouseleave(function () {
        $("#download-erweima").hide();
    });

    /*当鼠标移入二维码的时候，二维码的盒子显示*/
    $("#download-erweima").mouseenter(function () {
        $(this).show();
    }).mouseleave(function () {
        $(this).hide();
    });


    /*鼠标移入购物车的时候，显示的盒子*/
    $("#cart").mouseenter(function () {
        $("#hover-cart").slideDown(200);
    }).mouseleave(function () {
        $("#hover-cart").slideUp(400);
    })


    /*nav下拉栏*/
    $(".candown-list").mouseenter(function () {
        /*$(".nav-dropdown").slideDown(200);*/
        $(".nav-dropdown").show();

        $(".candown").mouseenter(function () {
            index = $(this).index();

            $(".toolbox").eq(index).show().siblings().hide();
        })
    }).mouseleave(function () {
        /*$(".nav-dropdown").slideUp(400);*/
        $(".nav-dropdown").hide();
    });

    $(".nav-dropdown").mouseenter(function () {
        $(this).show();
    }).mouseleave(function () {
        $(this).hide();
    });


    /*右测固定导航栏事件*/
    $("#phone-app").mouseenter(function () {
        $(".right-app").show();
    }).mouseleave(function () {
        $(".right-app").hide();
    });

    /*显示app二维码*/
    $(".right-app").mouseenter(function () {
        $(".right-app").show();
    }).mouseleave(function () {
        $(".right-app").hide();
    });

    /*返回顶部按钮*/
    $("#return-top").hide();

    /*当滚动条位置处于距顶部800px以下时，返回顶部链接出现*/
    $(window).scroll(function () {
        if ($(window).scrollTop() > 800) {
            $("#return-top").fadeIn(200);
        }else {
            $("#return-top").fadeOut(200);
        }
    });

    /*返回顶部事件*/
    $("#return-top").click(function () {
        $("body, html").animate({scrollTop: 0}, 500);
    });


    /*右侧固定导航栏结束*/



    /*轮播图开始*/
    /*右下角圆点事件*/
    var index = 0;
    $(".dot").click(function () {
        index = $(this).index(); // 获取点击li下标
        $(this).addClass("active").siblings().removeClass("active");
        $(".pic-item").eq(index).fadeIn().siblings().fadeOut();
    });

    /*下一张按钮事件*/
    $(".next").click(function () {
        index++;
        if (index > 3) {
            index = 0;
        }

        $(".pic-item").eq(index).fadeIn().siblings().fadeOut();
        $(".dot").eq(index).addClass("active").siblings().removeClass("active");
    });

    /*上一张点击事件*/
    $(".previous").click(function () {
        index--;
        if (index < 0) {
            index = 3;
        }

        $(".pic-item").eq(index).fadeIn().siblings().fadeOut();
        $(".dot").eq(index).addClass("active").siblings().removeClass("active");
    });

    /*自动轮播事件*/
    var auto = setInterval(function () {
        autoChange();
    }, 1000);

    function autoChange() {
        var autoIndex = 0;
        autoIndex = $(".active").index();

        autoIndex++;
        if (autoIndex > 3) { // 当轮播到最后一张返回第一张张
            autoIndex = 0;
        }

        $(".pic-item").eq(autoIndex).fadeIn().siblings().fadeOut();
        $(".dot").eq(autoIndex).addClass("active").siblings().removeClass("active");
    }

    /*鼠标移入的时候停止轮播*/
    $(".slideshow").mouseenter(function () {
        clearInterval(auto);
    }).mouseleave(function () {
        auto = setInterval(function () {
            autoChange();
        }, 3000);
    });
    /*轮播图结束*/


    /*左侧导航栏开始*/
    $(".sidebar-list").mouseenter(function () {
        index = $(this).index();
        $(".play-none").eq(index).show().siblings().hide();
    }).mouseleave(function () {
        $(".play-none").hide();
    });
    $(".play-none").mouseenter(function () {
        $(this).show();
    }).mouseleave(function () {
        $(this).hide();
    });
    /*左侧导航栏结束*/


    /*倒计时开始*/
    const countDown = () => {
        const endTime = new Date('2021/7/4 24:00:00').getTime(); // 设置结束时间
        const startTime = new Date().getTime(); // 开始时间
        const distanceTime = endTime - startTime; // 计算时间差
        const addZero = n => n<10? '0'+n: n; // 添加0

        // 小时
        var hour = Math.floor(distanceTime/ 1000/ 60/ 60% 24);
        var minute = Math.floor(distanceTime / 1000 / 60 % 60);
        var second = Math.floor(distanceTime / 1000 % 60);

        $(".hour").html(addZero(hour));
        $(".minute").html(addZero(minute));
        $(".sec").html(addZero(second));
    }

    setInterval(countDown, 1000);
    /*倒计时结束*/


});



window.onload = function () {
    const oulGood = document.getElementById("show-good");
    const oShow = document.getElementsByClassName("show-good-ul")[0];
    const aGoodLi = oShow.getElementsByTagName("li");
    const oDirection = document.getElementsByClassName("direction")[0];
    const oTurn = oDirection.getElementsByTagName("span");
    const oTurnLeft = document.getElementsByClassName("direc-left")[0];
    const oTurnRight = document.getElementsByClassName("direc-right")[0];

    const len = aGoodLi.length;
    const maxLeft = -((len - 4) * 246);
    const turnLeft = (aGoodLi[0].offsetWidth + 11) * 4;
    let turnIndex = 0;

    /*向左移动点击事件*/
    oTurnLeft.onclick = function () {
        turnIndex += turnLeft;

        if (turnIndex > 0) {
            turnIndex = 0;
        }

        toMove(oShow, 'left', turnIndex);
        /*oShow.style.left = turnIndex + 'px';*/

        oTurnLeft.className = (turnIndex === 0)?
            "it direc-left iconfont": "direc-left iconfont";
    };

    /*向右移动点击事件*/
    oTurnRight.onclick = function () {
        turnIndex -= turnLeft;

        if (turnIndex < maxLeft) {
            turnIndex = maxLeft;
        }

        toMove(oShow, 'left', turnIndex);
        /*oShow.style.left = turnIndex + 'px';*/

        oTurnRight.className = (turnIndex === maxLeft)?
            "it direc-left iconfont": "direc-left iconfont";

    }

    /*自动轮播事件*/
    clearInterval(autoUl);
    var autoUl = setInterval(function () {
        autoPlay();
    }, 4000);

    function autoPlay() {
        var changeLeft = 0;
        /*var cur = oShow.offsetWidth;

        var speed = (changeLeft - cur) / 7;
        speed = speed>0? Math.ceil(speed): Math.floor(speed);
        */
        changeLeft = oShow.offsetLeft;

        changeLeft -= turnLeft;


        /*if (changeLeft === 0) {
            oTurnLeft.className = "it direc-left iconfont";
            oTurnRight.className = "direc-left iconfont";
        }else if (changeLeft === maxLeft) {
            oTurnLeft.className = "direc-left iconfont";
            oTurnRight.className = "it direc-left iconfont";
        }else {
            oTurnLeft.className = "direc-left iconfont";
            oTurnRight.className = "direc-left iconfont";
        }*/

        if (Math.abs(changeLeft) === oShow.offsetWidth) {
            changeLeft = 0;
            toMove(oShow, 'left', 0);
        } else if (changeLeft < maxLeft) {
            changeLeft = maxLeft;
            toMove(oShow, 'left', maxLeft);
        }else {
            toMove(oShow, 'left', changeLeft);
        }

        /*oShow.style.left = changeLeft + 'px';*/
        /*oShow.style.left = cur + speed + 'px';*/

        for (let i = 0; i < oTurn.length; i++) {
            oTurn[i].className = "direc-left iconfont";

            if (changeLeft === 0) {
                oTurnLeft.className = "it direc-left iconfont";
            }else if (changeLeft === maxLeft) {
                oTurnRight.className = "it direc-left iconfont";
            }
        }

    }
    /*自动轮播事件结束*/

    /*鼠标移入停止轮播开始*/
    oulGood.onmouseover = function () {
      clearInterval(autoUl);
    };
    oulGood.onmouseout = function () {
        autoUl = setInterval(function () {
            autoPlay();
        }, 4000);
    };

    /*鼠标移入停止轮播结束*/



};
